import { BaseTexture, Texture, Sprite } from 'pixi.js'
export function parse(root) {
    const inlinkArr = []; // 先执行
    const array = [];
    if (!(root instanceof Object)) {
        return null;
    }
    function f(root, path, type = 'dir') {
        const res = {
            file: {},
            filename: '',
        };
        const { $, dir, uol, png, int16, int32, string, vector, sound } = root;
        res.filename = $.name;
        path.push($.name);
        if (type === 'png') {
            res.file.value = $.value;
            if (string) {
                const item = string.find(i => i.$.name === '_inlink');
                if (item) {
                    const { value } = item.$;
                    const pathArr = value.split('/');
                    inlinkArr.push((fullObj) => {
                        pathArr.forEach(str => {
                            fullObj = fullObj[str];
                        });
                        res.file.value = fullObj.value;
                    });
                }
            }
        }
        if (sound) {
            sound.forEach(r => {
                const { name, value } = r.$;
                res.file[name] = value;
            });
        }
        // 如果存在dir属性则证明是个文件夹。其他的都是undefined
        if (dir) {
            dir.forEach(r => {
                const { file, filename } = f(r, [...path], 'dir');
                res.file[filename] = file;
            });
        }
        if (int16) {
            int16.forEach(i => {
                const { name, value } = i.$;
                res.file[name] = Number(value);
            });
        }
        if (int32) {
            int32.forEach(i => {
                const { name, value } = i.$;
                res.file[name] = Number(value);
            });
        }
        if (string) {
            string.forEach(i => {
                const { name, value } = i.$;
                res.file[name] = String(value);
            });
        }
        if (vector) {
            vector.forEach(i => {
                const { name, value } = i.$;
                res.file[name] = parseVector(value);
            });
        }
        if (uol) {
            uol.forEach(i => {
                const objPath = path.slice(1); // path[0]不需要，因为是是输出的filename
                const { name, value } = i.$;
                const dotArr = value.split('\/');
                dotArr.forEach(str => {
                    if (/^\.\.$/.test(str)) {
                        objPath.pop();
                    }
                    else if (/^\.$/.test(str) === false) {
                        objPath.push(str);
                    }
                });
                // fullObj 最后生成的完整对象
                array.push((fullObj) => {
                    objPath.forEach(str => {
                        fullObj = fullObj[str];
                    });
                    res.file[name] = fullObj;
                });
            });
        }
        if (png) {
            png.forEach(i => {
                const { file, filename } = f(i, [...path], 'png');
                res.file[filename] = file;
            });
        }
        return res;
    }
    const res = f(root, []);
    inlinkArr.forEach(callback => {
        callback(res.file);
    });
    array.forEach(callback => {
        callback(res.file);
    });
    return res;
}


export function parseVector(str) {
    const sit = { x: 0, y: 0 };
    let xyArr = str.replace(/( |\(|\))/g, '').split(',');
    sit.x = Number(xyArr[0]);
    sit.y = Number(xyArr[1]);
    return sit;
}


export function calcOrigin(xArr, yArr) {
    let x = Math.min(...xArr);
    let y = Math.min(...yArr);
    x = x > 0?0: -x;
    y = y > 0?0: -y;
    return {
        x, y
    }   
}

export function calcInfo(xInfo, yInfo) {
    let width = 0;
    let height = 0;
    xInfo.forEach(item => {
        if (item.image) {
            let w = item.image.width + item.value;
            width = Math.max(w, width);
        }
    })
    yInfo.forEach(item => {
        if (item.image) {
            let h = item.image.height + item.value;
            height = Math.max(h, height);
        }
    })
    return {
        width,
        height
    }
}

export function loadBase64(img, prefix=true) {
    return new Promise((resolve) => {
        let image = new Image();
        if (prefix) {
            image.src = `data:image/gif;base64,${img}`
        } else {
            image.src = img;
        }
        image.onload = function() {
            resolve(image);
        }
    })
}

export function imageToSprite(image) {
    const baseTexture = new BaseTexture(image);
    const texture = new Texture(baseTexture);
    return new Sprite(texture);
}

export function imageToTexture(image) {
    const baseTexture = new BaseTexture(image);
    const texture = new Texture(baseTexture);
    return texture;
}

export function applyMixins(derivedCtor, constructor) {
    constructor.forEach((baseCtor) => {
      Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
        if (name !== "constructor") {
          Object.defineProperty(
            derivedCtor.prototype,
            name,
            (
              Object.getOwnPropertyDescriptor(baseCtor.prototype, name)
            )
          );
        }
      });
    });
  }

function parsePosition(str) {
    const Sit = {
        x: undefined, y: undefined
    }
    let xyArr = str.replace(/( |\(|\))/g, '').split(',');
    Sit.x = Number(xyArr[0]);
    Sit.y = Number(xyArr[1]);
    return Sit;
}

export function isMobile() {
    if (window.innerWidth <= 1024) {
        return true;
    } else {
        return false;
    }
}

export function noop(...argvs) {}